<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>维修管理</el-breadcrumb-item>
                <el-breadcrumb-item>维修记录管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="mtRecordFormData"  class="demo-form-inline">

                <el-form-item >
                    <el-input v-model="mtRecordFormData.shopName" placeholder="请输入维修厂"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-input v-model="mtRecordFormData.carName" placeholder="请输入汽车品牌"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-date-picker
                            v-model="mtRecordFormData.startDate"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            type="datetime"
                            placeholder="选择开始日期"
                            :picker-options="pickerOptions">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary"  @click="initmtRecordTable">查询</el-button>
                    <el-button type="primary"@click="addRecordFunction">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <!--<el-table-column
                            prop="id"
                            label="编号"
                            width="180"
                                >
                    </el-table-column>-->
                    <el-table-column
                            prop="recordId"
                            label="维修编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="carId"
                            label="汽车编号"
                            width="100">
                    </el-table-column>

                    <el-table-column
                            prop="tbCar.carName"
                            label="汽车品牌"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="carImg"
                            label="汽车样式">
                        <template   slot-scope="scope">
                            <img :src="scope.row.carImg"  min-width="70" height="80" />
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="tabmtShop.name"
                            label="维修厂"
                            width="180">
                    </el-table-column>
                    <!--<el-table-column
                            prop="maintainShopId"
                            label="维修厂">
                    </el-table-column>-->
                    <el-table-column
                            prop="addTime"
                            label="维修时间"
                            sortable="true"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="amount"
                            label="维修金额"
                            >
                    </el-table-column>
                    <el-table-column
                            prop="operator"
                            label="操作人">
                    </el-table-column>
                    <el-table-column
                            prop="operationTime"
                            label="操作时间"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="remark"
                            label="维修详情">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" @click="selectById(scope.row.id)" circle></el-button>
                            <el-button type="danger" icon="el-icon-delete" @click="del(scope.row.id)" circle></el-button>

                        </template>
                    </el-table-column>
                </el-table>
                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="7"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>

        </el-card>
        <!--                更新用户信息表单-->
        <el-dialog
                title="添加维修维修记录"
                :visible.sync="editmtRecordDialog"
                width="30%">
            <!--            model表单数据-->
            <el-form  label-width="80px" :model="mtRecordForm" :rules="mtRecordFormRules" ref="mtRecordFormRef">
                <el-form-item label="汽车id" prop="carId">
                    <el-input v-model="mtRecordForm.carId"></el-input>
                </el-form-item>
                <el-form-item label="维修厂" prop="maintainShopId">
                    <el-select v-model="mtRecordForm.maintainShopId" placeholder="请选择维修厂" @change="see(mtRecordForm.maintain_shop_id)">
                        <el-option
                                v-for="itemsss in shopArray"
                                :key="itemsss.id"
                                :label="itemsss.name"
                                :value="itemsss.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="汽车样式" prop="carImg">
                    <el-upload
                            class="avatar-uploader"
                            action="http://localhost:80/maintain/upload"
                            :headers="headers"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrlLog" :src="imageUrlLog" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

                <el-form-item label="维修时间" prop="addTime">
                    <el-date-picker
                            v-model="mtRecordForm.addTime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            type="datetime"
                            placeholder="选择日期时间"
                            align="right"
                            :picker-options="pickerOptions">
                    </el-date-picker>

                </el-form-item>
                <el-form-item label="维修金额" prop="amount">
                    <el-input v-model="mtRecordForm.amount"></el-input>
                </el-form-item>
                <el-form-item label="备注" >
                    <el-input v-model="mtRecordForm.remark"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                        <el-button @click="editmtRecordDialog = false">取 消</el-button>
                        <el-button type="primary" @click="editmtRecordBtn">确 定</el-button>
                    </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "MaintainRecord",
        data(){
//重复名自定义校验
            let validateAnotherMtRecorName = (rule, value, callback) => {
                //后台方法
                this.$http.post(`/maintain/record/selMtRecordCarId`,this.mtRecordForm).then(function (res) {
                    if (res && res.data.result.code === 0) {

                        callback()
                    } else if (res && res.data.result.code === 1) {
                        callback(new Error('这汽车已经在维修啦'))
                    }

                })
            }

            return {
                mtRecordFormData:{},
                tableData:[],
                currentPage:1,
                pageSize:5,
                total:0,
                //添加用户弹出框显示状态
                editmtRecordDialog:false,
                //添加用户表单信息
                mtRecordForm:{},
                //下拉框数据
                shopArray:[],
                //校验
                mtRecordFormRules:{
                    carId:[{required:true,message:"汽车不可为空",trigger:'blur'},
                        { validator: validateAnotherMtRecorName, trigger: 'blur'}],
                    maintainShopId:[{required:true,message:"必须选择维修厂",trigger:'blur'}],
                    addTime:[{required:true,message:"必须选择时间",trigger:'blur'}],
                    amount:[{ required: true, message: '必须输入金额',trigger: 'blur'},
                        ,{pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]{1,2}$)/,
                            message: "必须输入正整数"},]
                },
                //图片
                imageUrlLog:"",
                //获取请求头
                headers:{
                    token:sessionStorage.getItem("token")
                },
                //日期选择时间左边快捷键
                pickerOptions: {
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    },{
                        text: '一年前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 365);
                            picker.$emit('pick', date);
                        }
                    }],
                    disabledDate(time)
                    {
                        //return time.getTime() > Date.now() - 8.64e7;//如果没有后面的-8.64e7就是不可以选择今天的
                        return time.getTime() > Date.now();//可以选择当前时间

                    }
                },
            }
        },
        created() {
            this.initmtRecordTable();
            this.selectListAll();

        },
        methods:{
            //图片上传
            handleAvatarSuccess(res, file) {
                this.imageUrlLog =res.result.path;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 5MB!');
                }
                return isJPG && isLt2M;
            },
            see(te) {
                // console.log(te)
            },
            addRecordFunction(){
                this.editmtRecordDialog=true;
                this.mtRecordForm={};
            },
            initmtRecordTable(){
                //get方式不支持json数据
                var that=this;
                /*this.$http.post(`/maintain/record/select`,this.mtRecordFormData).then(function(resp){
                    that.tableData=resp.data.result.records;
                  // that.total=resp.data.result.total;
                });*/
                this.$http.post(`/maintain/record/initMtRecordTable/${this.currentPage}/${this.pageSize}`,this.mtRecordFormData).then(function(resp){
                    // console.log(resp.data.result.records)
                    that.tableData=resp.data.result.records;

                    that.total=resp.data.result.total;
                });
            },
            //删除
            del(id){
                var that=this;
                this.$http.delete(`/maintain/record/del/${id}`).then(function (resp) {
                    that.$message.success("删除成功");
                    //提交后刷新
                    that.initmtRecordTable();
                })
            },
            //修改回显
            selectById(id){
                this.editmtRecordDialog=true;
                var that=this;
                this.$http.get(`/maintain/record/selById/${id}`).then(function (resp) {
                    that.mtRecordForm=resp.data.result.MtRecord;
                })
            },
            //添加
            editmtRecordBtn(){
                var  that=this;
                //获取token中的登录人信息
                var username=sessionStorage.getItem("username");
                this.$refs.mtRecordFormRef.validate((valid)=>{
                    if (valid){//验证通过
                        //进行id判断,如果id不为空则回显数据,走修改,如果id为空,则走添加
                        if (this.mtRecordForm.id!=null) {
                            this.mtRecordForm.carImg=this.imageUrlLog
                            //有id就修改
                            this.$http.put(`/maintain/record/updateMtRecordById/`, this.mtRecordForm).then(function (r) {
                                // that.$message.success(r.data.message);
                                //消息通知
                                that.$message.success("修改成功");
                                //提交后关闭弹窗
                                that.editmtRecordDialog = false;
                                //提交后刷新
                                that.initmtRecordTable();


                            })
                        }else {
                            this.mtRecordForm.carImg=this.imageUrlLog
                            //没有id就插入
                            this.$http.post(`/maintain/record/insertMtRecord/${username}`,this.mtRecordForm).then(function (r) {
                                // that.$message.success(r.data.message);
                                //提交后关闭弹窗
                                that.editmtRecordDialog=false;
                                //提交后刷新
                                that.initmtRecordTable();
                                //消息通知
                                that.$message({
                                    message: '添加成功',
                                    type: 'success'
                                });
                            })
                        }
                         that.mtRecordForm={};

                    }else {
                        this.$message.error('好好填一下信息呀,亲');
                    }

                })

            },
            //添加下拉框数据
            selectListAll(){
                var that=this;
                this.$http.get("/maintain/shop/selectListAll").then(function (r) {
                    that.shopArray=r.data.result.mtShops;
                    // console.log(that.shopArray)
                })

            },
            //分页
            handleSizeChange(v){
                this.pageSize=v;
                this.initmtRecordTable();
            },
            //分页
            handleCurrentChange(v){
                this.currentPage=v;
                this.initmtRecordTable();
            }
        }
    }
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

</style>